using System.Diagnostics;
using Microsoft.VisualBasic;
using System;
using System.Data;
using System.Collections;
using System.Security.Cryptography;
using System.IO;
using System.Text;


namespace Brainsoft.Core.Security
{
	public class PublicKeySecurityManager
	{
		
		public MyRSAInfo Encrypt(string strData)
		{
			MyRSAInfo myrsa = new MyRSAInfo();
			CspParameters p = new CspParameters();
			p.Flags = CspProviderFlags.UseMachineKeyStore;
			myrsa.Parameters = p;
			RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(p);
			byte[] data = rsa.Encrypt(Encoding.Unicode.GetBytes(strData), false);
			myrsa.PublicKey = rsa.ToXmlString(false);
			myrsa.PrivateKey = rsa.ToXmlString(true);
			myrsa.Data = data;
			return myrsa;
		}
		
		public byte[] Decrypt(MyRSAInfo myrsa)
		{
			RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(myrsa.Parameters);
			rsa.FromXmlString(myrsa.PublicKey);
			byte[] data = rsa.Decrypt(myrsa.Data, false);
			return data;
		}
		
	}
	
	public class MyRSAInfo
	{
		
		public string PublicKey;
		public string PrivateKey;
		public CspParameters Parameters;
		public byte[] Data;
	}
}


//private void Button1_Click(System.Object sender, System.EventArgs e)
//{
//    PublicKeySecurityHelper s = new PublicKeySecurityHelper();
//    myrsa = s.Encrypt(TextBox1.Text);
//    MessageBox.Show("Su info ha sido encryptada");
//}

//private void Button2_Click(System.Object sender, System.EventArgs e)
//{
//    byte[] data;
//    PublicKeySecurityHelper s = new PublicKeySecurityHelper();
//    data = s.Decrypt(myrsa);
//    MessageBox.Show(System.Text.ASCIIEncoding.Unicode.GetString(data));
//}